package cn.shuibo.config;

import cn.shuibo.entity.SecretKeyEntity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 从配置中获取配置数据
 * @ClassName:  SecretKeyEntity   
 * @Description:加解密配置参数
 * @author: Ccl
 * @date:   2021年11月27日 下午2:25:56      
 * @Copyright:
 */
@Data
@ConfigurationProperties(prefix = "rsa.encrypt")
@Configuration
public class MutiSecretKeyConfig {
	
	/**
	 * appid用户标识
	 */
	private String appid;
	/**
	 * 是否开启加解密日志
	 */
	private boolean showLog = false;

	private boolean open = true;

	private Set<SecretKeyEntity> secretKeys;

	private HashMap<String, SecretKeyEntity> userCache = new HashMap<>();

	public SecretKeyEntity getByAppid(String appid){
		if(secretKeys!=null && secretKeys.size()>0 && userCache.size()==0){
			for(SecretKeyEntity entity: secretKeys){
				userCache.put(entity.getAppid(),entity);
			}
		}
		return userCache.get(appid);
	}

	public SecretKeyEntity getCurrentAppid(){
		return getByAppid(this.appid);
	}

}
